/**
 * Created by jianglinjie on Mon Sep 18 2017
 * @flow
 */
'use strict';

import './db'; //连接db
import './redis'; //连接redis

import Koa from 'koa';
import json from 'koa-json';
import bodyparser from 'koa-bodyparser';
import logger from 'koa-logger';
import router from './routes';
import { errorMiddleware } from './utils/ErrorUtils';
import cors from 'koa2-cors';

const app = new Koa();

//error handler test
app.use(errorMiddleware);

// middlewares
app.use(bodyparser({
  enableTypes:['json', 'form', 'text']
}));
app.use(json());

if (process.env.NODE_ENV === 'development') {
  app.use(logger());
}

app.use(cors({
  allowMethods: ['GET', 'PUT', 'POST', 'DELETE', 'HEAD', 'OPTIONS', 'PATCH']
}));
// router
app.use(router.routes()).use(router.allowedMethods);

module.exports = app
